define(function(require){
	var $=require("jq");
	var Dialog=require("/pinzhiV2.0/js/pz_use_dialog.js");
	//验证增加修改地址
 	var newAddressInputs=$("#new-address-box .ver");
	//验证成功失败的提示
	function ver_tips(thisObj,text,boolean){
		if(boolean){
			$(thisObj).attr("data-ok","true").next().show().addClass("ok").text(text);
		}else{
			$(thisObj).attr("data-ok","false").next().show().removeClass("ok").text(text);
		};
	};
	//验证收货人
	newAddressInputs.eq(0).blur(function(){
		var val=$(this).val();
		if(val.isUserName()){
			ver_tips(this,"用户名正确！",true);
		}else{
			ver_tips(this,"用户名不准确！",false);
		};
	});
 	//验证详细地址
	newAddressInputs.eq(4).blur(function(){
		var val=$(this).val();
		if(val!=""){
			ver_tips(this,"地址正确！",true);
		}else{
			ver_tips(this,"地址输入错误！",false);
		};
	});
 	//验证邮编
	newAddressInputs.eq(5).blur(function(){
		var val=$(this).val();
		if(val.isPostcode()){
			ver_tips(this,"正确！",true);
		}else{
			ver_tips(this,"错误！",false);
		};
	});
 	//验证手机
	newAddressInputs.eq(6).blur(function(){
		var val=$(this).val();
		if(val.isMobile()){
			ver_tips(this,"正确！",true);
		}else{
			ver_tips(this,"错误！",false);
		};
	});
 	
 	//验证邮件
	newAddressInputs.eq(7).blur(function(){
		var val=$(this).val();
		if(val.isEmailAddress()){
			ver_tips(this,"正确！",true);
		}else{
			ver_tips(this,"错误！",false);
		};
	});
	newAddressInputs.eq(8).blur(function(){
		var val=$(this).val();
		if(val.isID()){
			ver_tips(this,"正确！",true);
		}else{
			ver_tips(this,"错误！",false);
		};
	});
	//验证地区输入
	$("#provinceId").change(function(){
		if(this.value!=-1){
			$(this).attr("data-ok","true");
		}else{
			$(this).attr("data-ok","false");
		};
		$("#cityId").attr("data-ok","false");
		$("#areaId").attr("data-ok","false");
	});
	$("#cityId").change(function(){
		if(this.value!=-1){
			$(this).attr("data-ok","true");
		}else{
			$(this).attr("data-ok","false");
		};
		$("#areaId").attr("data-ok","false");
	});
	$("#areaId").change(function(){
		if(this.value!=-1){
			$(this).attr("data-ok","true");
		}else{
			$(this).attr("data-ok","false");
		};
	});                
	var verSize=newAddressInputs.size(),lastV=0;
	
	
	
	
	
	
	
	
	
	function initEditForm(){
		$.get("/vipCenter/initEditForm.do",function(responseText){
			var data = responseText['data'];
			var areaData = responseText['areaData'];
			
			var provinces = areaData['provinces'];
			var citys = areaData['citys'];
			var areas = areaData['areas'];
			
			var id = data['id'];
			var name = data['name'];
			var address = data['address'];
			var zipcode = data['zipcode'];
			var phone = data['phone'];
			var areaPath = data['areaPath'];
			
			$('#consigneeID').val(id);
			$('#consignee').val(name);
			var provinces_option = "<option value='-1'>--省份/直辖市--</option>";
			for(var i=0;i<provinces.length;i++){
				provinces_option = provinces_option + "<option value='"+provinces[i].id+"'>"+provinces[i].name+"</option>";
			}
			$('#provinceId').html(provinces_option);
			
			var city_option = "<option value='-1'>-市-</option>";
			for(var i=0;i<citys.length;i++){
				city_option = city_option + "<option value='"+citys[i].id+"'>"+citys[i].name+"</option>";
			}
			$('#cityId').html(city_option);
			
			var area_option = "<option value='-1'>-市-</option>";
			for(var i=0;i<areas.length;i++){
				area_option = area_option + "<option value='"+areas[i].id+"'>"+areas[i].name+"</option>";
			}
			$('#areaId').html(area_option);
			$('#detailAddress').val(address);
			$('#postalCode').val(zipcode);
			$('#mobile').val(phone);
		},"json");
	}
	
	initEditForm();
	
	$('.editBtn').on('click', function() {
		
  	  	var consigneeID = $(this).attr('consigneeID');
  	    $.post("/vipCenter/initEditForm.do",{"consigneeID":consigneeID},function(responseText){
  	    	var data = responseText['data'];
			var areaData = responseText['areaData'];
			
			var provinces = areaData['provinces'];
			var citys = areaData['citys'];
			var areas = areaData['areas'];
			
			var id = data['id'];
			var name = data['name'];
			var address = data['address'];
			var zipcode = data['zipcode'];
			var phone = data['phone'];
			var areaPath = data['areaPath'];
			var provinceID = data['provinceID'];
			var cityID = data['cityID'];
			var areaID = data['areaID'];
			
			$('#consigneeID').val(id);
			$('#consignee').val(name);
			var provinces_option = "<option value='-1'>--省份/直辖市--</option>";
			for(var i=0;i<provinces.length;i++){
				var selectTag = "";
				if(provinceID == provinces[i].id){
					selectTag = "selected='selected'";
				}
				provinces_option = provinces_option + "<option "+selectTag+" value='"+provinces[i].id+"'>"+provinces[i].name+"</option>";
			}
			$('#provinceId').html(provinces_option).attr("data-ok","true");
			
			var city_option = "<option value='-1'>-市-</option>";
			for(var i=0;i<citys.length;i++){
				var selectTag = "";
				if(cityID == citys[i].id){
					selectTag = "selected='selected'";
				}
				city_option = city_option + "<option "+selectTag+" value='"+citys[i].id+"'>"+citys[i].name+"</option>";
			}
			$('#cityId').html(city_option).attr("data-ok","true");
			
			var area_option = "<option value='-1'>-市-</option>";
			for(var i=0;i<areas.length;i++){
				var selectTag = "";
				if(areaID == areas[i].id){
					selectTag = "selected='selected'";
				}
				area_option = area_option + "<option "+selectTag+" value='"+areas[i].id+"'>"+areas[i].name+"</option>";
			}
			$('#areaId').html(area_option).attr("data-ok","true");
			$('#detailAddress').val(address);
			$('#postalCode').val(zipcode);
			$('#mobile').val(phone);
			newAddressInputs.blur();
			//验证是否提交表单
		},"json");
  	});
	
	$('.deleteBtn').on('click',function(){
		var consigneeID = $(this).attr('consigneeID');
		if(confirm("是否删除地址？")){
			$.getJSON("/vipCenter/deleteConsignee.do",{"consigneeID":consigneeID},function(data){
				var success = data.success;
				if(success != 1){
					
					var tips=new Dialog({
						  type:"alert",
						  text:"提示",
						  tipsText:data.message,
						  buttonSureText:"知道了", 
						  mask:0.8,
						  isDrag:true               //开起拖动
						  });
				}else{
					var tips=new Dialog({
						  type:"alert",
						  text:"提示",
						  tipsText:"删除地址成功！",
						  buttonSureText:"知道了", 
						  mask:0.8,
						  isDrag:true               //开起拖动
						  });
					document.location.reload();
				}
			},"json");
		}
	});
	
	$('#addBtn').on('click', function() {
		var consigneeID = $('#consigneeID').val();
		var receiverName = $('#consignee').val();
		var regionID = $('#areaId').val();
		var address = $('#detailAddress').val();
		var postCode = $('#postalCode').val();
		var mobilePhone = $('#mobile').val();
		//验证是否提交表单

		newAddressInputs.each(function(i,o){
				if($(this).attr("data-ok")=="true"){
					lastV=i;
				}else{
					return false;
				};
		});
		if(lastV<6){
			var tips=new Dialog({
				  type:"alert",
				  text:"提示",
				  tipsText:"没有填写完整！",
				  buttonSureText:"继续填写", 
				  mask:0.3,
				  isDrag:true               //开起拖动
				  });
			return false;
		};

		$.post("/vipCenter/updateConsignee.do",{"consigneeID":consigneeID,"receiverName":receiverName,"regionID":regionID,"address":address,"postCode":postCode,"mobilePhone":mobilePhone},function(data){
			var success = data.success;
			if(success == 1){
				var tips=new Dialog({
					  type:"alert",
					  text:"提示",
					  tipsText:"更新成功！",
					  buttonSureText:"知道了",
					  callBack:function(){
							$('#consigneeID').val('');
							$('#consignee').val('');
							$('#detailAddress').val('');
							$('#postalCode').val('');
							$('#mobile').val('');
							window.location.href = '/vipCenter/userAddressSet.do?menu_id=2'; 
					  },
					  mask:0.3,
					  isDrag:true               //开起拖动
					  });
			}else{
				var tips=new Dialog({
					  type:"alert",
					  text:"提示",
					  tipsText:"更新失败，请联系管理员！",
					  buttonSureText:"知道了", 
					  mask:0.3,
					  isDrag:true               //开起拖动
					  });
				return;
			};
			

		},"json");
	}); 
	
	$('#provinceId').on('change', function() {
		var provinceId = $(this).val();
		$.getJSON("/vipCenter/areaSelect.do",{"regionId":provinceId},function(data){
			var lengthNum = data.length;
			var optionHtml = "<option value='-1'>-市-</option>";
			for(var i=0;i<lengthNum;i++){
				optionHtml = optionHtml + "<option value='"+data[i].id+"'>"+data[i].name+"</option>";
			}
			$('#cityId').html('').html(optionHtml);
		},"json");
		$('#areaId').html('').html("<option value='-1'>-县/区-</option>");
	});
	$('#cityId').on('change', function() {
		var cityId = $(this).val();
		$.getJSON("/vipCenter/areaSelect.do",{"regionId":cityId},function(data){
			var lengthNum = data.length;
			var optionHtml = "<option value='-1'>-县/区-</option>";
			for(var i=0;i<lengthNum;i++){
				optionHtml = optionHtml + "<option value='"+data[i].id+"'>"+data[i].name+"</option>";
			}
			$('#areaId').html('').html(optionHtml);
		},"json");
	});
});